package ru.gelin.android.bells;

import java.util.Date;

import ru.gelin.android.bells.timers.Timer;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

public class TimersListActivity extends NowActivity implements Constants {
    
    /** Storage of the timers */
    TimerStorage timers;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timers_list);
        timers = TimerStorage.getInstance(this);
        
        ListView timersList = (ListView)findViewById(R.id.timers_list);
        registerForContextMenu(timersList);
        timersList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                editTimer((int)id);
            }
        });
        
        setVolumeControlStream(AudioManager.STREAM_ALARM);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        loadTimers();
        if (timers.size() > 0) {
            findViewById(R.id.add_timer_warn).setVisibility(View.GONE);
            findViewById(R.id.timers_list).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.add_timer_warn).setVisibility(View.VISIBLE);
            findViewById(R.id.timers_list).setVisibility(View.GONE);
        }
    }
    
    @Override
    public void onPause() {
        super.onPause();
        timers.save();
        timers.scheduleAlarm();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.timers_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_add_timer:
            addTimer();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, 
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.timers_list_item, menu);
        
        View itemView = ((AdapterContextMenuInfo)menuInfo).targetView;
        TextView timerName = (TextView)itemView.findViewById(R.id.timer_name);
        menu.setHeaderTitle(timerName.getText());
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        switch (item.getItemId()) {
        case R.id.menu_edit_timer:
            editTimer((int)info.id);
            return true;
        case R.id.menu_delete_timer:
            deleteTimer((int)info.id);
            return true;
        case R.id.menu_start_now_timer:
            startNowTimer((int)info.id);
            return true;
        }
        return super.onContextItemSelected(item);
    }    
    
    void loadTimers() {
        now = new Date();   //we updating and normalizing the timer according actually current time
        timers.load();
        timers.normalize(now);
        ListView list = (ListView)findViewById(R.id.timers_list);
        ListAdapter adapter = new TimersListAdapter(this, timers, now);
        list.setAdapter(adapter);
    }
    
    void addTimer() {
        Intent intent = new Intent(this, AddTimerActivity.class);
        startActivity(intent);
    }
    
    void editTimer(int id) {
        Intent intent = new Intent(this, EditTimerActivity.class);
        intent.setData(new Uri.Builder().scheme(TIMER_SCHEME).
                opaquePart(String.valueOf(id)).build());
        startActivity(intent);
    }
    
    void deleteTimer(int id) {
        timers.delete(id);
        timers.save();
        loadTimers();
    }
    
    void startNowTimer(int id) {
        Timer timer = timers.select(id);
        timer.setStartTime(now);
        timer.normalize(now);
        timers.update(timer);
        timers.save();
        loadTimers();
    }

}